/**
 * 
 */
package com.eis.admin.mvc.controller;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.eis.admin.mvc.bean.AdminLogin;
import com.eis.admin.mvc.common.utils.MVCConstants;
import com.eis.admin.mvc.controller.base.MVCBaseController;
import com.eis.persist.dao.UserDAO;
import com.eis.persist.dao.impl.UserDAOImpl;
import com.eis.persist.model.user.PersistUser;

/**
 * @author sh-panwei01
 *
 */
@Controller
public class AdminLoginController extends MVCBaseController {
	private static final Logger logger = LoggerFactory.getLogger(AdminLoginController.class); 
	
	@SuppressWarnings("unused")
	private Jaxb2Marshaller jaxb2Mashaller;
	private UserDAO userDAO;

	public void setFunctionDataSource(JdbcTemplate functionDataSource) {
		this.functionDataSource = functionDataSource;
		userDAO = new UserDAOImpl(this.functionDataSource);
	}

	public void setJaxb2Mashaller(Jaxb2Marshaller jaxb2Mashaller) {
		this.jaxb2Mashaller = jaxb2Mashaller;
	}

    @RequestMapping(value="/adminLogin", method = RequestMethod.GET)
    public ModelAndView showAdminLogin(Model model, HttpSession session) {
    	if (session.getAttribute(MVCConstants.USER_SESSION_KEY) != null) {
			return new ModelAndView("redirect:" + appPrefix + "/adminHome");
		} else {
			logger.debug("showAdminLogin");
	        return new ModelAndView("adminLogin", "command", new AdminLogin());
		}
    }
    
    @RequestMapping(value="/reLogin")
    public String reLogin(Model model, HttpSession session) {
    	return "reLogin";
    }
    
    @RequestMapping(value="/adminHome")
    public String adminHome(Model model, HttpSession session) {
    	PersistUser persistUser = (PersistUser)session.getAttribute(MVCConstants.USER_SESSION_KEY);
    	
    	model.addAttribute("userName", persistUser.getName());
    	return "adminHome";
    }
    
    @RequestMapping(value="/logout")
    public String logout(HttpSession session) {
    	PersistUser persistUser = (PersistUser)session.getAttribute(MVCConstants.USER_SESSION_KEY);
    	if (persistUser != null) {
    		session.setAttribute(MVCConstants.USER_SESSION_KEY, null);
    	}
    	return "redirect:" + appPrefix + "/adminLogin";
    }
    
    @RequestMapping(value="/submitLogin")
    public ModelAndView adminLogin(Model model, @ModelAttribute("adminLogin") AdminLogin adminLogin, HttpSession session) {
    	if (userDAO.userLogon(adminLogin.getUserName(), adminLogin.getEncodePassword())) {
    		session.setAttribute(MVCConstants.USER_SESSION_KEY, null);
    		// load User and save to session
    		PersistUser persistUser = userDAO.getUserWithConfig(adminLogin.getUserName());
    		
    		session.setAttribute(MVCConstants.USER_SESSION_KEY, persistUser);
    		
    		return new ModelAndView("redirect:" + appPrefix + "/adminHome");
    	} else {
    		if (session.getAttribute(MVCConstants.USER_SESSION_KEY) != null) {
    			return new ModelAndView("redirect:" + appPrefix + "/adminHome");
    		} else {
    			model.addAttribute("errorMessage", "Username or password is not correct!");
        		return new ModelAndView("adminLogin", "command", new AdminLogin());
    		}
    	}
    }
}
